package com.pai4j.search.controller;
import com.pai4j.common.bean.PAIPageResponseBeanUtil;
import com.pai4j.common.bean.PAIResponseBean;
import com.pai4j.common.bean.PAIResponseBeanUtil;
import com.pai4j.common.helper.SessionHelper;
import com.pai4j.log.annotation.OperationLog;
import com.pai4j.remote.search.SearchEngineClient;
import com.pai4j.search.service.es.Pai4jElasticSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class SearchClientController implements SearchEngineClient {

    @Autowired
    private Pai4jElasticSearchService pai4jElasticSearchService;

    /**
     * 聊天消息搜索
     *
     * @param query 关键词
     * @param type  搜索内容：空代表聚合搜索，搜全部；可以置顶具体的消息类型（聊天、公众号文章、用户）
     *
     * @param size
     * @return
     */
    @OperationLog(
        module = "搜索管理",
        operation = "内容搜索",
        description = "用户进行内容搜索",
        recordParams = true
    )
    public PAIPageResponseBeanUtil query(@RequestParam(value = "q", required = false) String query,
                                         @RequestParam(value = "type", required = false) Integer type,
                                         @RequestParam(value = "userId", required = false) String userId,
                                         @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                         @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
       return pai4jElasticSearchService.query(type, userId, query, page, size);
    }



    /**
     *  创建索引
     **/
    @OperationLog(
            module = "搜索管理",
            operation = "创建索引",
            description = "管理员创建搜索索引",
            recordParams = false
    )
    public PAIResponseBean<Boolean> createChatMessageIndex() {
        boolean createIndexResult = pai4jElasticSearchService.createIndex();
        return PAIResponseBeanUtil.success(createIndexResult);
    }
    /**
     *  删除索引
     **/
    @OperationLog(
            module = "搜索管理",
            operation = "删除索引",
            description = "管理员删除搜索索引",
            recordParams = false
    )
    public PAIResponseBean<Boolean> deleteIndex() {
        boolean createIndexResult = pai4jElasticSearchService.deleteIndex();
        return PAIResponseBeanUtil.success(createIndexResult);

    }
}
